package com.ktgj.customer.service.impl;


import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

import com.alibaba.nacos.shaded.com.google.gson.Gson;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.ktgj.common.core.annotation.Excel;
import com.ktgj.common.core.constant.CustomerConstants;
import com.ktgj.common.core.utils.DateUtils;
import com.ktgj.customer.domain.*;
import com.ktgj.customer.domain.vo.*;
import com.ktgj.customer.mapper.*;
import com.ktgj.customer.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 会员卡Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-07-30
 */
@Service
public class CustomerCardServiceImpl implements ICustomerCardService 
{
    @Autowired
    private CustomerCardMapper customerCardMapper;
    @Autowired
    private CardMapper cardMapper;
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private DepartmentMapper departmentMapper;
    @Autowired
    private BusinessOrderMapper businessOrderMapper;
    @Autowired
    private BusinessOrderSubMapper businessOrderSubMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ICustomerService customerService;
    @Autowired
    private GuaranteeMapper guaranteeMapper;
    @Autowired
    private IDepositBusinessService depositBusinessService;
    @Autowired
    private IPointRecordService pointRecordService;
    @Autowired
    private BusinessRefundMapper businessRefundMapper;
    @Autowired
    private BankMapper bankMapper;
    @Autowired
    private ILoginService loginService;
    @Resource
    private ReturncardMapper returncardMapper;

    /**
     * 查询会员卡
     * 
     * @param customerCardId 会员卡ID
     * @return 会员卡
     */
    @Override
    public CustomerCard selectCustomerCardById(Long customerCardId)
    {
        return customerCardMapper.selectCustomerCardById(customerCardId);
    }

    @Override
    public CustomerCard selectCustomerCard(String cardCode) {
        return customerCardMapper.selectCustomerCard(cardCode);
    }

    /**
     * 查询会员卡列表
     * 
     * @param customerCard 会员卡
     * @return 会员卡
     */
    @Override
    public List<CustomerCard> selectCustomerCardList(CustomerCard customerCard)
    {
        return customerCardMapper.selectCustomerCardList(customerCard);
    }

    /**
     * 新增会员卡
     * 
     * @param customerCard 会员卡
     * @return 结果
     */
    @Override
    public int insertCustomerCard(CustomerCard customerCard)
    {
        customerCard.setCreateTime(DateUtils.getNowDate());
        return customerCardMapper.insertCustomerCard(customerCard);
    }

    /**
     * 修改会员卡
     * 
     * @param customerCard 会员卡
     * @return 结果
     */
    @Override
    public int updateCustomerCard(CustomerCard customerCard)
    {
        customerCard.setUpdateTime(DateUtils.getNowDate());
        return customerCardMapper.updateCustomerCard(customerCard);
    }

    /**
     * 批量删除会员卡
     * 
     * @param customerCardIds 需要删除的会员卡ID
     * @return 结果
     */
    @Override
    public int deleteCustomerCardByIds(Long[] customerCardIds)
    {
        return customerCardMapper.deleteCustomerCardByIds(customerCardIds);
    }

    /**
     * 删除会员卡信息
     * 
     * @param customerCardId 会员卡ID
     * @return 结果
     */
    @Override
    public int deleteCustomerCardById(Long customerCardId)
    {
        return customerCardMapper.deleteCustomerCardById(customerCardId);
    }

    /**
     * 查询vip通道订单(远程)
     * */
    @Override
    public CustomerCard selectVipOrder(Long customerId) {
        return customerCardMapper.selectVipOrder(customerId);
    }

    @Override
    public List<CustomerCard> queryCardIdByCustomerId(Long customerId) {

        return customerCardMapper.queryCardIdByCustomerId(customerId);
    }

    /**
     * 售卡量统计
     * */
    @Override
    public List<StatisticsCardSalesVo> statisticsCardSales(CardParameterVo cardParameterVo) {
        cardParameterVo.pagesNum = (cardParameterVo.pagesNum - 1) * cardParameterVo.barsNum;
        List<StatisticsCardSalesVo> statisticsCardSalesVoList = new ArrayList<>();
        List<CustomerCard> customerCardList = customerCardMapper.statisticsCardSales(cardParameterVo.pagesNum, cardParameterVo.barsNum);
        if (customerCardList != null && customerCardList.size() > 0){
            List<Long> cardId = new ArrayList<>();

            for (CustomerCard customerCard : customerCardList) {
                //卡号
                if (cardParameterVo.cardNum != null && !cardParameterVo.cardNum.equals("")){
                    if (!customerCard.getCardCode().equals(cardParameterVo.cardNum)){
                        cardId.add(customerCard.getCustomerCardId());
                    }
                }

                //卡级别筛选
                if (cardParameterVo.cardLevel != null && !cardParameterVo.cardLevel.equals("")){
                    Card card = cardMapper.cardLevel(customerCard.getCardId());
                    if (card.getCardLevel() != cardParameterVo.cardLevel){
                        cardId.add(customerCard.getCustomerCardId());
                    }
                }
                //状态
                if (cardParameterVo.state != null && !cardParameterVo.state.equals("")){
                    if (!customerCard.getCardStatus().equals(cardParameterVo.state)){
                        cardId.add(customerCard.getCustomerCardId());
                    }
                }
                //会员手机号
                if (cardParameterVo.customerPhone != null && !cardParameterVo.customerPhone.equals("")){
                    Customer customer = customerMapper.selectCustomerById(customerCard.getCustomerId());
                    if (!customer.getContractPhoneOne().equals(cardParameterVo.customerPhone)){
                        cardId.add(customerCard.getCustomerCardId());
                    }
                }
            }
            HashSet hashSet = new HashSet(cardId);
            cardId.clear();
            cardId.addAll(hashSet);

            if (cardId != null && cardId.size() > 0){
                for (CustomerCard customerCard : customerCardList) {
                    for (Long aLong : cardId) {
                        if (customerCard.getCustomerCardId() != aLong){
                            StatisticsCardSalesVo statisticsCardSalesVo = new StatisticsCardSalesVo();
                            statisticsCardSalesVo.cardId = customerCard.getCustomerCardId();
                            Card card = cardMapper.cardLevel(customerCard.getCardId());
                            statisticsCardSalesVo.cardName = card.getCardSectionName();
                            statisticsCardSalesVo.cardNum = customerCard.getCardCode();
                            statisticsCardSalesVo.cardLevel = card.getCardLevel();
                            statisticsCardSalesVo.salesAmount = card.getPrice();
                            statisticsCardSalesVo.purchaseTime = customerCard.getCreateTime();
                            statisticsCardSalesVo.expireDate = customerCard.getInvalidDate();
                            statisticsCardSalesVo.state = customerCard.getCardStatus();
                            Customer customer = customerMapper.selectCustomerById(customerCard.getCustomerId());
                            statisticsCardSalesVo.customerName = customer.getCustomerName();
                            statisticsCardSalesVo.customerPhone = customer.getContractPhoneOne();
                            statisticsCardSalesVoList.add(statisticsCardSalesVo);
                        }
                    }
                }
            }
            else {
                for (CustomerCard customerCard : customerCardList) {
                    StatisticsCardSalesVo statisticsCardSalesVo = new StatisticsCardSalesVo();
                    statisticsCardSalesVo.cardId = customerCard.getCustomerCardId();
                    Card card = cardMapper.cardLevel(customerCard.getCardId());
                    statisticsCardSalesVo.cardName = card.getCardSectionName();
                    statisticsCardSalesVo.cardNum = customerCard.getCardCode();
                    statisticsCardSalesVo.cardLevel = card.getCardLevel();
                    statisticsCardSalesVo.salesAmount = card.getPrice();
                    statisticsCardSalesVo.purchaseTime = customerCard.getCreateTime();
                    statisticsCardSalesVo.expireDate = customerCard.getInvalidDate();
                    statisticsCardSalesVo.state = customerCard.getCardStatus();
                    Customer customer = customerMapper.selectCustomerById(customerCard.getCustomerId());
                    statisticsCardSalesVo.customerName = customer.getCustomerName();
                    statisticsCardSalesVo.customerPhone = customer.getContractPhoneOne();
                    statisticsCardSalesVoList.add(statisticsCardSalesVo);
                }
            }

            return statisticsCardSalesVoList;
        }
        else {
            return null;
        }
    }

    /**
     * 退卡列表
     * */
    @Override
    public List<CheckOutVo> checkOutList(CardParameterVo cardParameterVo) {
        List<CheckOutVo> checkOutVoList = new ArrayList<>();
        List<CustomerCard> customerCardList = customerCardMapper.checkOutList(cardParameterVo.cardNum);
        if (customerCardList != null && customerCardList.size() > 0){
            List<Long> cardId = new ArrayList<>();

            for (CustomerCard customerCard : customerCardList) {
                //卡级别筛选
                if (cardParameterVo.cardLevel != null && !cardParameterVo.cardLevel.equals("")){
                    Card card = cardMapper.cardLevel(customerCard.getCardId());
                    if (card.getCardLevel() != cardParameterVo.cardLevel){
                        cardId.add(customerCard.getCustomerCardId());
                    }
                }
                //状态
                if (cardParameterVo.state != null && !cardParameterVo.state.equals("")){
                    if (!customerCard.getCardStatus().equals(cardParameterVo.state)){
                        cardId.add(customerCard.getCustomerCardId());
                    }
                }
                //会员手机号
                if (cardParameterVo.customerPhone != null && !cardParameterVo.customerPhone.equals("")){
                    Customer customer = customerMapper.selectCustomerById(customerCard.getCustomerId());
                    if (!customer.getContractPhoneOne().equals(cardParameterVo.customerPhone)){
                        cardId.add(customerCard.getCustomerCardId());
                    }
                }
            }
            HashSet hashSet = new HashSet(cardId);
            cardId.clear();
            cardId.addAll(hashSet);

            if (cardId != null && cardId.size() > 0){
                for (CustomerCard customerCard : customerCardList) {
                    for (Long aLong : cardId) {
                        if (customerCard.getCustomerCardId() != aLong){
                            CheckOutVo checkOutVo = new CheckOutVo();
                            checkOutVo.cardId = customerCard.getCustomerCardId();
                            checkOutVo.cardRefundTime = customerCard.getCancelDate();
                            Card card = cardMapper.cardLevel(customerCard.getCardId());
                            checkOutVo.cardName = card.getCardSectionName();
                            checkOutVo.cardNum = customerCard.getCardCode();
                            checkOutVo.cardLevel = card.getCardLevel();
                            checkOutVo.cardRefundAmount = customerCard.getCancelAmount();
                            Customer customer = customerMapper.selectCustomerById(customerCard.getCustomerId());
                            checkOutVo.cuatomerName = customer.getCustomerName();
                            checkOutVo.cuatomerPhone = customer.getContractPhoneOne();
                            if (customerCard.getCancelDate() != null && customerCard.getCancelAmount() == null){
                                checkOutVo.cardStatus = "退款申请";
                            }
                            else if (customerCard.getCancelDate() != null && customerCard.getCancelAmount() != null){
                                checkOutVo.cardStatus = "退款成功";
                            }
                            checkOutVoList.add(checkOutVo);
                        }
                    }
                }
            }
            else {
                for (CustomerCard customerCard : customerCardList) {
                    CheckOutVo checkOutVo = new CheckOutVo();
                    checkOutVo.cardId = customerCard.getCustomerCardId();
                    checkOutVo.cardRefundTime = customerCard.getCancelDate();
                    Card card = cardMapper.cardLevel(customerCard.getCardId());
                    checkOutVo.cardName = card.getCardSectionName();
                    checkOutVo.cardNum = customerCard.getCardCode();
                    checkOutVo.cardLevel = card.getCardLevel();
                    checkOutVo.cardRefundAmount = customerCard.getCancelAmount();
                    Customer customer = customerMapper.selectCustomerById(customerCard.getCustomerId());
                    checkOutVo.cuatomerName = customer.getCustomerName();
                    checkOutVo.cuatomerPhone = customer.getContractPhoneOne();
                    if (customerCard.getCancelDate() != null && customerCard.getCancelAmount() == null){
                        checkOutVo.cardStatus = "退款申请";
                    }
                    else if (customerCard.getCancelDate() != null && customerCard.getCancelAmount() != null){
                        checkOutVo.cardStatus = "退款成功";
                    }
                    checkOutVoList.add(checkOutVo);
                }
            }

            return checkOutVoList;
        }
        else {
            return null;
        }
    }

    /**
     * 续费列表
     * */
    @Override
    public List<RenewalVo> renewalList(CardParameterVo cardParameterVo) {
        List<RenewalVo> renewalVoList = new ArrayList<>();
        List<CustomerCard> customerCardList = customerCardMapper.renewalList(cardParameterVo.cardNum);
        if (customerCardList != null && customerCardList.size() > 0){
            List<Long> cardId = new ArrayList<>();

            for (CustomerCard customerCard : customerCardList) {
                //卡级别筛选
                if (cardParameterVo.cardLevel != null && !cardParameterVo.cardLevel.equals("")){
                    Card card = cardMapper.cardLevel(customerCard.getCardId());
                    if (card.getCardLevel() != cardParameterVo.cardLevel){
                        cardId.add(customerCard.getCustomerCardId());
                    }
                }
                //状态
                if (cardParameterVo.state != null && !cardParameterVo.state.equals("")){
                    if (!customerCard.getCardStatus().equals(cardParameterVo.state)){
                        cardId.add(customerCard.getCustomerCardId());
                    }
                }
                //会员手机号
                if (cardParameterVo.customerPhone != null && !cardParameterVo.customerPhone.equals("")){
                    Customer customer = customerMapper.selectCustomerById(customerCard.getCustomerId());
                    if (!customer.getContractPhoneOne().equals(cardParameterVo.customerPhone)){
                        cardId.add(customerCard.getCustomerCardId());
                    }
                }
            }
            HashSet hashSet = new HashSet(cardId);
            cardId.clear();
            cardId.addAll(hashSet);

            if (cardId != null && cardId.size() > 0){
                for (CustomerCard customerCard : customerCardList) {
                    for (Long aLong : cardId) {
                        if (customerCard.getCustomerCardId() != aLong){
                            RenewalVo renewalVo = new RenewalVo();
                            renewalVo.cardId = customerCard.getCustomerCardId();
                            Card card = cardMapper.cardLevel(customerCard.getCardId());
                            renewalVo.cardName = card.getCardSectionName();
                            renewalVo.cardNum = customerCard.getCardCode();
                            CustomerCard customerCardNew = customerCardMapper.newRenewal(customerCard.getCustomerCardId());
                            Card cardNew = cardMapper.cardLevel(customerCardNew.getCardId());
                            renewalVo.newCardName = cardNew.getCardSectionName();
                            renewalVo.newCardNum = customerCardNew.getCardCode();
                            renewalVo.renewalTime = customerCard.getXfDate();
                            if (customerCard.getCarddeptId() != null){
                                Department department = departmentMapper.selectDepartment(customerCard.getCarddeptId());
                                renewalVo.renewalDepartment = department.getDeptName();
                            }
                            renewalVo.importEmployees = customerCard.getActiveEmployeeId();
                            renewalVo.takeEffect = customerCard.getCardStatus();
                            renewalVo.effectiveDate = customerCard.getBusinessDate();
                            renewalVoList.add(renewalVo);
                        }
                    }
                }
            }
            else {
                for (CustomerCard customerCard : customerCardList) {
                    RenewalVo renewalVo = new RenewalVo();
                    renewalVo.cardId = customerCard.getCustomerCardId();
                    Card card = cardMapper.cardLevel(customerCard.getCardId());
                    renewalVo.cardName = card.getCardSectionName();
                    renewalVo.cardNum = customerCard.getCardCode();
                    CustomerCard customerCardNew = customerCardMapper.newRenewal(customerCard.getCustomerCardId());
                    Card cardNew = cardMapper.cardLevel(customerCardNew.getCardId());
                    renewalVo.newCardName = cardNew.getCardSectionName();
                    renewalVo.newCardNum = customerCardNew.getCardCode();
                    renewalVo.renewalTime = customerCard.getXfDate();
                    if (customerCard.getCarddeptId() != null){
                        Department department = departmentMapper.selectDepartment(customerCard.getCarddeptId());
                        renewalVo.renewalDepartment = department.getDeptName();
                    }
                    renewalVo.importEmployees = customerCard.getActiveEmployeeId();
                    renewalVo.takeEffect = customerCard.getCardStatus();
                    renewalVo.effectiveDate = customerCard.getBusinessDate();
                    renewalVoList.add(renewalVo);
                }
            }

            return renewalVoList;
        }
        else {
            return null;
        }
    }

    /**
     * 退续费列表
     * */
    @Override
    public List<RenewalVo> checkOutRenewalList(CardParameterVo cardParameterVo) {
        List<RenewalVo> renewalVoList = new ArrayList<>();
        List<CustomerCard> customerCardList = customerCardMapper.renewalList(cardParameterVo.cardNum);
        if (customerCardList != null && customerCardList.size() > 0){
            List<Long> cardId = new ArrayList<>();

            for (CustomerCard customerCard : customerCardList) {
                //卡级别筛选
                if (cardParameterVo.cardLevel != null && !cardParameterVo.cardLevel.equals("")){
                    Card card = cardMapper.cardLevel(customerCard.getCardId());
                    if (card.getCardLevel() != cardParameterVo.cardLevel){
                        cardId.add(customerCard.getCustomerCardId());
                    }
                }
                //状态
                if (cardParameterVo.state != null && !cardParameterVo.state.equals("")){
                    if (!customerCard.getCardStatus().equals(cardParameterVo.state)){
                        cardId.add(customerCard.getCustomerCardId());
                    }
                }
                //会员手机号
                if (cardParameterVo.customerPhone != null && !cardParameterVo.customerPhone.equals("")){
                    Customer customer = customerMapper.selectCustomerById(customerCard.getCustomerId());
                    if (!customer.getContractPhoneOne().equals(cardParameterVo.customerPhone)){
                        cardId.add(customerCard.getCustomerCardId());
                    }
                }
            }
            HashSet hashSet = new HashSet(cardId);
            cardId.clear();
            cardId.addAll(hashSet);

            if (cardId != null && cardId.size() > 0){
                for (CustomerCard customerCard : customerCardList) {
                    for (Long aLong : cardId) {
                        if (customerCard.getCustomerCardId() != aLong){
                            RenewalVo renewalVo = new RenewalVo();
                            renewalVo.cardId = customerCard.getCustomerCardId();
                            Card card = cardMapper.cardLevel(customerCard.getCardId());
                            renewalVo.cardName = card.getCardSectionName();
                            renewalVo.cardNum = customerCard.getCardCode();
                            CustomerCard customerCardNew = customerCardMapper.newRenewal(customerCard.getCustomerCardId());
                            Card cardNew = cardMapper.cardLevel(customerCardNew.getCardId());
                            renewalVo.newCardName = cardNew.getCardSectionName();
                            renewalVo.newCardNum = customerCardNew.getCardCode();
                            renewalVo.renewalTime = customerCard.getXfDate();
                            if (customerCard.getCarddeptId() != null){
                                Department department = departmentMapper.selectDepartment(customerCard.getCarddeptId());
                                renewalVo.renewalDepartment = department.getDeptName();
                            }
                            renewalVo.importEmployees = customerCard.getActiveEmployeeId();
                            renewalVo.takeEffect = customerCard.getCardStatus();
                            renewalVo.effectiveDate = customerCard.getBusinessDate();
                            if (customerCard.getCancelDate() != null && customerCard.getCancelAmount() == null){
                                renewalVo.cardReturnStatus = "退款申请";
                            }
                            else if (customerCard.getCancelDate() != null && customerCard.getCancelAmount() != null){
                                renewalVo.cardReturnStatus = "退款成功";
                            }
                            renewalVoList.add(renewalVo);
                        }
                    }
                }
            }
            else {
                for (CustomerCard customerCard : customerCardList) {
                    RenewalVo renewalVo = new RenewalVo();
                    renewalVo.cardId = customerCard.getCustomerCardId();
                    Card card = cardMapper.cardLevel(customerCard.getCardId());
                    renewalVo.cardName = card.getCardSectionName();
                    renewalVo.cardNum = customerCard.getCardCode();
                    CustomerCard customerCardNew = customerCardMapper.newRenewal(customerCard.getCustomerCardId());
                    Card cardNew = cardMapper.cardLevel(customerCardNew.getCardId());
                    renewalVo.newCardName = cardNew.getCardSectionName();
                    renewalVo.newCardNum = customerCardNew.getCardCode();
                    renewalVo.renewalTime = customerCard.getXfDate();
                    if (customerCard.getCarddeptId() != null){
                        Department department = departmentMapper.selectDepartment(customerCard.getCarddeptId());
                        renewalVo.renewalDepartment = department.getDeptName();
                    }
                    renewalVo.importEmployees = customerCard.getActiveEmployeeId();
                    renewalVo.takeEffect = customerCard.getCardStatus();
                    renewalVo.effectiveDate = customerCard.getBusinessDate();
                    if (customerCard.getCancelDate() != null && customerCard.getCancelAmount() == null){
                        renewalVo.cardReturnStatus = "退款申请";
                    }
                    else if (customerCard.getCancelDate() != null && customerCard.getCancelAmount() != null){
                        renewalVo.cardReturnStatus = "退款成功";
                    }
                    renewalVoList.add(renewalVo);
                }
            }

            return renewalVoList;
        }
        else {
            return null;
        }
    }

    /**
     * 查询会员的所有会员卡
     * */
    @Override
    public CustomerCard customerCardList(Long customerId) {
        List<CustomerCard> customerCardList = customerCardMapper.CustomerCardList(customerId);
        if (customerCardList != null && customerCardList.size() > 0){
            for (CustomerCard customerCard : customerCardList) {
                Card card = cardMapper.cardLevel(customerCard.getCardId());
                customerCard.setCardLevel(card.getCardLevel());
            }
            customerCardList = customerCardList.stream().sorted(Comparator.comparing(CustomerCard::getCardLevel)).collect(Collectors.toList());
            return customerCardList.get(0);
        }
        else {
            return null;
        }
    }
    /**
     * 查询数据每个部门的售卡条数
     * @param cardSaleRecords
     * @return
     */
    @Override
    public List<CardSaleRecords> selectCardSaleNum(CardSaleRecords cardSaleRecords)
    {
        List<CardSaleRecords> cardSaleRecordsList = new ArrayList<>();
        List<Department> departmentList;
        if (cardSaleRecords.getDepId() != null)
        {
            departmentList = departmentMapper.departmentList(cardSaleRecords.getDepId());
        } else
        {
            departmentList = departmentMapper.departmentList(null);
        }

        if (departmentList.size() > 0)
        {
            for (Department department : departmentList)
            {
                CardSaleRecords records = new CardSaleRecords();
                records.setDepId(department.getDeptId());
                records.setDepName(department.getDeptName());
                List<BusinessOrder> businessOrders = businessOrderMapper.businessOrderList(department.getDeptId(), cardSaleRecords.getStartDate(), cardSaleRecords.getEndDate());
                if (businessOrders.size() > 0)
                {
                    records.setCount(Long.valueOf(businessOrders.size()));
                    cardSaleRecordsList.add(records);
                }
            }
        }
        return cardSaleRecordsList;
    }
    /**
     * 查询单个部门的售卡记录
     * @param cardSaleRecords
     * @return
     */
    @Override
    public List<BusinessOrder> selectCardSaleRecords(CardSaleRecords cardSaleRecords)
    {
        List<BusinessOrder> businessOrders = businessOrderMapper.businessOrderByDeptId(cardSaleRecords);
        if (businessOrders.size() > 0)
        {
            for (BusinessOrder businessOrder : businessOrders)
            {
                List<BusinessOrderSub> lstBusinessOrderSub = businessOrderSubMapper.businessOrderSubByOrderId(businessOrder.getOrderId());
                BusinessOrderSub businessOrderSub = businessOrderSub = lstBusinessOrderSub.get(0);
                if (businessOrderSub.getCustomerCardId() != null)
                {
                    CustomerCard customerCard = customerCardMapper.selectCustomerCardById(businessOrderSub.getCustomerCardId());
                    if (customerCard != null)
                    {
                        if (customerCard.getCardStatus().equals(CustomerConstants.valid.getValue()))
                        {
                            businessOrder.setCardStatus("已生效");
                        }
                        if (customerCard.getCardStatus().equals(CustomerConstants.invalid.getValue()))
                        {
                            businessOrder.setCardStatus("已失效");
                        }
                        if (customerCard.getCardStatus().equals(CustomerConstants.refunds.getValue()))
                        {
                            businessOrder.setCardStatus("已退卡");
                        }
                        if (customerCard.getCardStatus().equals(CustomerConstants.frozen.getValue()))
                        {
                            businessOrder.setCardStatus("已冻结");
                        }
                        if (businessOrder.getState().equals(CustomerConstants.payd.getValue()))
                        {
                            businessOrder.setOrderState("已支付");
                        }
                        if (businessOrder.getState().equals(CustomerConstants.waitPay.getValue()))
                        {
                            businessOrder.setOrderState("待支付");
                        }
                        if (businessOrder.getState().equals(CustomerConstants.cancelPay.getValue()))
                        {
                            businessOrder.setOrderState("支付取消");
                        }
                        if (businessOrder.getState().equals(CustomerConstants.failPay.getValue()))
                        {
                            businessOrder.setOrderState("支付失败");
                        }
                        businessOrder.setCardCode(customerCard.getCardCode());
                    }
                }

                Department department = departmentMapper.selectDepartmentById(businessOrder.getEmployeeDeptid());
                if (department != null)
                {
                    businessOrder.setDepName(department.getDeptName());
                }

                Employee employee = employeeMapper.employeeNameList(businessOrder.getEmployeeId());

                if (employee != null)
                {
                    businessOrder.setEmployee(employee.getEmployeeName());
                }
                businessOrder.setCardSource("自动");
                businessOrder.setCardSaleDate(businessOrder.getCreateTime());
                businessOrder.setCardType(businessOrderSub.getCardName());
                if (businessOrderSub.getAssistProductId() != null && businessOrderSub.getAssistProductId().equals("no"))
                {
                    businessOrder.setCardStatus("false");
                } else if (businessOrderSub.getAssistProductId() != null && businessOrderSub.getAssistProductId().equals("yes"))
                {
                    businessOrder.setCardStatus("true");
                }
            }
        }
        return businessOrders;
    }

    /**
     * 退卡详情
     * */
    @Override
    public CardRefundDetailsVo cardRefundDetails(Long id) throws Exception {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        CardRefundDetailsVo cardRefundDetailsVo = new CardRefundDetailsVo();
        Returncard returncard = returncardMapper.selectReturncardById(id);
        BusinessOrderSub businessOrderSub = businessOrderSubMapper.businessOrderSubByID(returncard.getCardId());
        BusinessOrder businessOrder = null;
        if (businessOrderSub != null){
            businessOrder = businessOrderMapper.selectBusinessOrderByOrderId(businessOrderSub.getOrderId());
        }

        if (businessOrder != null){
            cardRefundDetailsVo.setOrderNum(businessOrderSub.getOrderNum());
            if (businessOrderSub != null){
                CustomerCard customerCard = customerCardMapper.selectCustomerCardById(businessOrderSub.getCustomerCardId());
                if (customerCard != null){
                    cardRefundDetailsVo.setCardNum(customerCard.getCardCode());
                    if (customerCard.getCreateTime() != null){
                        cardRefundDetailsVo.setTime(format.format(customerCard.getCreateTime()));
                    }
                    if (customerCard.getCarddmansId() != null){
                        Employee employee = employeeMapper.selectEmployeeById(customerCard.getCarddmansId());
                        if (employee != null){
                            cardRefundDetailsVo.setCardSeller(employee.getEmployeeName());
                            if (employee.getDeptId() != null){
                                Department department = departmentMapper.selectDepartmentById(employee.getDeptId());
                                if (department != null){
                                    cardRefundDetailsVo.setDept(department.getDeptName());
                                }
                            }
                        }
                    }
                    List<Guarantee> guaranteeList = guaranteeMapper.guaranteeByCardNum(customerCard.getCardCode());
                    if (guaranteeList != null && guaranteeList.size() > 0){
                        cardRefundDetailsVo.setOpen("true");
                    }
                    else {
                        cardRefundDetailsVo.setOpen("false");
                    }
                }
            }
            try {
                CustomerIdVo customerIdVo = new CustomerIdVo();
                customerIdVo.setCustomerId(businessOrder.getCustomerId());
                customerIdVo = customerService.customerId(customerIdVo);
                if (customerIdVo != null){
                    cardRefundDetailsVo.setName(customerIdVo.getCustomerName());
                }
            } catch (Exception e){
                throw new Exception(e.getMessage());
            }

            BigDecimal bigDecimal = depositBusinessService.queyUserDepositBusiness(businessOrder.getCustomerId());
            if (bigDecimal != null && bigDecimal.compareTo(new BigDecimal(0)) >= 0){
                cardRefundDetailsVo.setWithdraw(bigDecimal.toString());
            }
            Long aLong = pointRecordService.queryUserCreditsNum(businessOrder.getCustomerId());
            if (aLong != null && aLong >= 0){
                cardRefundDetailsVo.setIntegral(aLong.toString());
            }

            if (businessOrder.getPrice() != null){
                cardRefundDetailsVo.setOpenCard(businessOrder.getPrice().toString());
            }

            BigDecimal usedMoney = new BigDecimal(0);
            if (businessOrder.getUsedMoney() != null){
                usedMoney = usedMoney.add(businessOrder.getUsedMoney());
                cardRefundDetailsVo.setTheFullAmountOfTheBankCardShouldBeRefunded(businessOrder.getUsedMoney().toString());
            }

            if (businessOrder.getUsedDeposit() != null){
                if (usedMoney.compareTo(new BigDecimal(0)) == 0){
                    usedMoney = usedMoney.add(businessOrder.getUsedDeposit());
                }
                else {
                    usedMoney = usedMoney.subtract(businessOrder.getUsedDeposit());
                }
                cardRefundDetailsVo.setRefundableInFull(businessOrder.getUsedDeposit().toString());
            }
            cardRefundDetailsVo.setTotalAmountToBeRefunded(usedMoney.toString());

            BusinessRefund businessRefund = businessRefundMapper.businessRefundByOrderNum(businessOrder.getOrdernum());
            if (businessRefund != null){
                if (businessRefund.getPenaltguaranteemoney() != null){
                    cardRefundDetailsVo.setMemberPremiums(businessRefund.getPenaltguaranteemoney().toString());
                }
                if (businessRefund.getDepreciationPrice() != null){
                    cardRefundDetailsVo.setDepreciation(businessRefund.getDepreciationPrice().toString());
                }
                if (businessRefund.getEmployeeguaranteemoney() != null){
                    cardRefundDetailsVo.setTheEmployeePaysThePremium(businessRefund.getEmployeeguaranteemoney().toString());
                }

                cardRefundDetailsVo.setRefundMethod(businessRefund.getPayType());

                if (businessRefund.getRefundBank() != null){
                    Bank bank = bankMapper.selectBankByBankId(Long.valueOf(businessRefund.getRefundBank()));
                    if (bank != null){
                        cardRefundDetailsVo.setBank(bank.getBankName());
                    }
                }

                cardRefundDetailsVo.setUsername(businessRefund.getBankcardHolder());
                cardRefundDetailsVo.setCardNumber(businessRefund.getBankcardId());
                cardRefundDetailsVo.setAccountOpening(businessRefund.getBankcardOpenCity());
                cardRefundDetailsVo.setReasonForRefund(businessRefund.getRemark());
                cardRefundDetailsVo.setRemark(businessRefund.getRemark());
            }
        }
        Gson gson = new Gson();
        List<String> lstRemark =  gson.fromJson(returncard.getRemarks(),ArrayList.class);
        cardRefundDetailsVo.setLstRemark(lstRemark);
        cardRefundDetailsVo.setDepreciationmoney(returncard.getDepreciationmoney()==null?"":returncard.getDepreciationmoney().toString());
        cardRefundDetailsVo.setReason(returncard.getReason());
        cardRefundDetailsVo.setSmallChange("0");//不可提现零钱
        cardRefundDetailsVo.setVoucher(returncard.getGiftbalance()==null?"": returncard.getGiftbalance().toString());//礼品券
        cardRefundDetailsVo.setCount("0");//随行次数
        cardRefundDetailsVo.setDeductPlusDeductTheFullAmount(returncard.getPlusmoney().toString());//扣plus抵扣全额
        cardRefundDetailsVo.setDeductGiftCertificateForFullDeduction(returncard.getGiftmoney()==null?"":returncard.getGiftmoney().toString());//扣礼品券抵扣全额
        cardRefundDetailsVo.setDeductionOfTheNumberOfRewardTickets(returncard.getFlightmoney()==null?"":returncard.getFlightmoney().toString());//扣回馈送机票数
        cardRefundDetailsVo.setOther("0");//其他
        cardRefundDetailsVo.setDeductionOfExperienceFee(returncard.getPhysicalexaminationfee()==null?"":returncard.getPhysicalexaminationfee().toString());//扣体验费
        cardRefundDetailsVo.setDeductionOfRegistrationFee(returncard.getRegistrationfee()==null?"":returncard.getRegistrationfee().toString());//扣挂号费
        cardRefundDetailsVo.setTheNumberOfTimesTheSelfOperatedHallIsUsed(returncard.getPartnerviphallnum()==null?"":returncard.getPartnerviphallnum().toString());//自营厅使用次数
        cardRefundDetailsVo.setPhysicalCard("无");
        cardRefundDetailsVo.setMemberPremiums(returncard.getPenaltguaranteemoney()==null?"":returncard.getPenaltguaranteemoney().toString());
        cardRefundDetailsVo.setDeductionOfDidiDrivingVoucherFee("0");//扣滴滴代驾券费
        cardRefundDetailsVo.setDeductionOfMedicalExpenses("0");//扣家医费
        cardRefundDetailsVo.setDeductRemainingPlus(returncard.getPlusbalance()==null?"":returncard.getPlusbalance().toString());//扣除剩余plus
        cardRefundDetailsVo.setDeductRemainingGiftVouchers("0");//扣除剩余礼品
        cardRefundDetailsVo.setFlightMoney(returncard.getFlightmoney()==null?"":returncard.getFlightmoney().toString());
        cardRefundDetailsVo.setEmployeeguaranteemoney(returncard.getEmployeeguaranteemoney()==null?new BigDecimal(0):returncard.getEmployeeguaranteemoney());
        cardRefundDetailsVo.setNumberTimesSelfOperatedHallUsed(returncard.getPartnerviphallnum()==null?0l:returncard.getPartnerviphallnum());
        cardRefundDetailsVo.setSecurityCheckTimes(returncard.getVipchannelnum()==null?0l:returncard.getVipchannelnum());
        cardRefundDetailsVo.setPromotionalCost(returncard.getSalesmoney()==null?new BigDecimal(0):returncard.getSalesmoney());
        cardRefundDetailsVo.setRefundMethod(returncard.getRefundmethod());
        cardRefundDetailsVo.setJdDoctor(returncard.getJdDoctor());
        List<DailyRecordVo> dailyRecordVoList = new ArrayList<>();
        cardRefundDetailsVo.setDailyRecordVoList(dailyRecordVoList);//退卡日志信息
        return cardRefundDetailsVo;
    }

    /**
     * 修改退卡详情信息
     * */
    @Transactional
    @Override
    public int updateCardRefund(HttpServletRequest request, CardRefundDetailsVo cardRefundDetailsVo) throws Exception {
//        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
//
//        Employee loginEmployee = loginService.getLoginEmployee(request);
//        if (loginEmployee != null){
//            throw new Exception("登录已过期");
//        }
//
//        BusinessOrder businessOrder = new BusinessOrder();
//        if (cardRefundDetailsVo.getOpenCard() != null && !cardRefundDetailsVo.getOpenCard().equals("")){
//            businessOrder.setPrice(new BigDecimal(cardRefundDetailsVo.getOpenCard()));
//        }
//        if (cardRefundDetailsVo.getTheFullAmountOfTheBankCardShouldBeRefunded() != null &&
//                !cardRefundDetailsVo.getTheFullAmountOfTheBankCardShouldBeRefunded().equals("")){
//            businessOrder.setUsedMoney(new BigDecimal(cardRefundDetailsVo.getTheFullAmountOfTheBankCardShouldBeRefunded()));
//        }
//        if (cardRefundDetailsVo.getRefundableInFull() != null &&
//                !cardRefundDetailsVo.getRefundableInFull().equals("")){
//            businessOrder.setUsedDeposit(new BigDecimal(cardRefundDetailsVo.getRefundableInFull()));
//        }
//        businessOrder.setOrdernum(cardRefundDetailsVo.getOrderNum());
//        int i = businessOrderMapper.updateBusinessOrder(businessOrder);
//        if (i <= 0){
//            throw new Exception("修改订单失败");
//        }
//
//        BusinessRefund businessRefund = new BusinessRefund();
//        if (cardRefundDetailsVo.getMemberPremiums() != null && !cardRefundDetailsVo.getMemberPremiums().equals("")){
//            businessRefund.setPenaltguaranteemoney(new BigDecimal(cardRefundDetailsVo.getMemberPremiums()));
//        }
//        if (cardRefundDetailsVo.getDepreciation() != null && !cardRefundDetailsVo.getDepreciation().equals("")){
//            businessRefund.setDepreciationPrice(new BigDecimal(cardRefundDetailsVo.getDepreciation()));
//        }
//        if (cardRefundDetailsVo.getPromotionalCost() != null && !cardRefundDetailsVo.getPromotionalCost().equals("")){
//            businessRefund.setSalesmoney(new BigDecimal(cardRefundDetailsVo.getPromotionalCost()));
//        }
//        if (cardRefundDetailsVo.getTheEmployeePaysThePremium() != null &&
//                !cardRefundDetailsVo.getTheEmployeePaysThePremium().equals("")){
//            businessRefund.setEmployeeguaranteemoney(new BigDecimal(cardRefundDetailsVo.getTheEmployeePaysThePremium()));
//        }
//        if (cardRefundDetailsVo.getNumberTimesSelfOperatedHallUsed() != null &&
//                !cardRefundDetailsVo.getNumberTimesSelfOperatedHallUsed().equals("")){
//            businessRefund.setPartnerviphallnum(Long.valueOf(cardRefundDetailsVo.getNumberTimesSelfOperatedHallUsed()));
//        }
//        if (cardRefundDetailsVo.getSecurityCheckTimes() != null && !cardRefundDetailsVo.getSecurityCheckTimes().equals("")){
//            businessRefund.setVipchannelnum(Long.valueOf(cardRefundDetailsVo.getSecurityCheckTimes()));
//        }
//
//        businessRefund.setPayType(cardRefundDetailsVo.getRefundMethod());
//
//        if (cardRefundDetailsVo.getBank() != null && !cardRefundDetailsVo.getBank().equals("")){
//            Bank bank = bankMapper.selectBankLike("%" + cardRefundDetailsVo.getBank() + "%");
//            if (bank != null){
//                businessRefund.setRefundBank(bank.getBankId().toString());
//            }
//        }
//
//        cardRefundDetailsVo.setUsername(businessRefund.getBankcardHolder());
//        cardRefundDetailsVo.setCardNumber(businessRefund.getBankcardId());
//        cardRefundDetailsVo.setAccountOpening(businessRefund.getBankcardOpenCity());
//        cardRefundDetailsVo.setReasonForRefund(businessRefund.getRemark());
//        cardRefundDetailsVo.setRemark(businessRefund.getRemark());
//
//        if (businessOrder != null){
//            BusinessRefund businessRefund = businessRefundMapper.businessRefundByOrderNum(businessOrder.getOrdernum());
//            if (businessRefund != null){
//
//            }
//        }
//
//        List<DailyRecordVo> dailyRecordVoList = new ArrayList<>();
//        cardRefundDetailsVo.setDailyRecordVoList(dailyRecordVoList);//退卡日志信息
//        return cardRefundDetailsVo;
        return 0;
    }
}
